
import MesManager from "src/_T/Mes/MesManager";
import { EntityState } from "../GeneralScripts/GameDefine";
import PlayerBee from "../HoneyBee/EntityHoneyBee/PlayerBee";
import CameraControl from "./CameraControl";
import Desktopspeed from "./Desktopspeed";
import { E_AD_PlayerEvent } from "./PlayerEvent";

export class PlayerControl extends Laya.Script3D {

    /**玩家状态 */
    //m_PlayerState: PlayerState = PlayerState.PS_Empty_Handed_Mode;

    m_hitResult = new Laya.HitResult();
    m_RayObj: Laya.Sprite3D;
    MoveDir: Laya.Vector3 = null;
    MoveSpeed: number = 1;
    RotateSpeed: number = 0.2;
    m_SelfObj: Laya.Sprite3D;
    logicSpeed: Laya.Vector2 = Laya.Vector2.ZERO;
    roleDirection: number;
    renderDir: Laya.Quaternion = new Laya.Quaternion(0, 0, 0);
    //transform: Laya.Transform3D;
    QuaternionZero: Laya.Quaternion = new Laya.Quaternion(0, 0, 0, 0);
    /**玩家状态控制 */
    m_PlayerBeeCon: PlayerBee;
    ISPKScne: boolean = false;
    Speed: number = 50;

    onAwake() {
        /**添加事件 */
        MesManager.on(E_AD_PlayerEvent.PlayerMove, this, this.Player_Move);
        this.m_SelfObj = this.owner as Laya.Sprite3D;
        CameraControl.Instance.SetTarget(this.m_SelfObj);
        //MapCameraCon.Instance.SetFollowCameraTarget(this.m_SelfObj);
        this.renderDir = this.QuaternionZero;
    }
    onStart() {
        this.m_PlayerBeeCon = this.m_SelfObj.addComponent(PlayerBee);
        this.m_PlayerBeeCon.IsPKScene = this.ISPKScne;
    }
    onUpdate() {
        /**旋转 */
        if (this.renderDir != this.QuaternionZero) {
            let Ro = new Laya.Quaternion(0, 0, 0, 0);
            Laya.Quaternion.lerp(this.m_SelfObj.transform.rotation, this.renderDir, 0.2, Ro);
            this.m_SelfObj.transform.rotation = Ro.clone();
        }
        /**移动 */
        if (this.logicSpeed != Laya.Vector2.ZERO) {
            let Fo = new Laya.Vector3();
            this.m_SelfObj.transform.getForward(Fo);
            Fo.x *= -1;
            Fo.z *= -1;
            Laya.Vector3.normalize(Fo, Fo);
            let Pos = new Laya.Vector3();
            Laya.Vector3.scale(Fo, ((Laya.timer.delta / 1000) * this.m_PlayerBeeCon.m_TempAttributeBase.m_Speed), Pos);
            Laya.Vector3.add(this.m_SelfObj.transform.position, Pos, Pos);
            this.m_SelfObj.transform.position = Pos.clone();
        } else {
            this.renderDir = this.QuaternionZero;
        }
        CameraControl.Instance.UpdateCamera();
    }

    /**玩家移动 */
    public Player_Move(_dir: number) {
        if (_dir > 120) {
            this.logicSpeed = Laya.Vector2.ZERO;
            this.renderDir = this.QuaternionZero;
            this.m_PlayerBeeCon.m_AnimatorManager.logicSpeed = Laya.Vector2.ZERO.clone();
            this.m_PlayerBeeCon.m_AnimatorManager.renderDir = this.QuaternionZero.clone();
            this.m_PlayerBeeCon.m_AnimatorManager.ChangeState(EntityState.Idle);
        }
        else {
            this.roleDirection = _dir * 3;
            let Pos = Desktopspeed.Instance.GetSpeed(this.roleDirection).clone();
            Laya.Vector2.scale(Pos, this.MoveSpeed, Pos);
            this.logicSpeed = Pos.clone();
            let _renderDir: Laya.Vector3 = Desktopspeed.Instance.ChangeGameVectorToVector3(this.logicSpeed).clone();
            _renderDir.x *= -1;
            let RenderDir: Laya.Quaternion = new Laya.Quaternion(0, 0, 0, 0);
            Laya.Quaternion.rotationLookAt(_renderDir, new Laya.Vector3(0, 1, 0), RenderDir);
            this.renderDir = RenderDir.clone();
        }
    }
}